package com.example.shirodemo.filter;

import com.example.shirodemo.config.RedisConfig;
import com.example.shirodemo.service.RedisService;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;

@Component
public class MyCustomFilter extends BasicHttpAuthenticationFilter {


    @Autowired
    RedisService redisService;

    /**
     * 执行登录认证
     *
     * @param request
     * @param response
     * @param mappedValue
     * @return
     */
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        try {
            System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,");
            String token = request.getParameter("token");
            if(token==null){
                return false;
            }
            String redisToken = redisService.get(RedisConfig.PREFIX_USER_TOKEN + token);
            if(redisToken!=null&&redisToken.equals(token)){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

}
